package com.zyf.EasyNet.app.control;

import com.zyf.EasyNet.model.Result;
import com.zyf.EasyNet.model.User;
import com.zyf.EasyNet.model.UserLoginInfo;
import com.zyf.EasyNet.param.state.Code;
import com.zyf.EasyNet.service.UserService;
import com.zyf.EasyNet.util.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @first_author zyflzz
 * @gmt_created 2022/4/19
 * @gmt_modified 2022/4/19
 */
@Controller
public class StartControl {

    private static final Logger LOG = LoggerFactory.getLogger(StartControl.class);

    @Autowired
    private Global G;

    @Autowired
    private UserService userService;

    @Autowired
    private Code CODE;

    @PostConstruct
    public void init() {
        LOG.info("StartControl START");
    }

    @GetMapping(path = "/introduction")  // introduction
    public String introduction(Model model) {
        return "introduction";
    }

    @GetMapping(path = "/homePage")
    public String homePage() {
        return "homePage";
    }

    @GetMapping(path = "/sign")
    public String regPage(Model model) {
        return "sign";
    }

    @PostMapping(path = "/sign")
    public String register(@RequestParam String userName,
                           @RequestParam String password,
                           HttpServletRequest request,
                           HttpServletResponse response,
                           Map<String, Object> returnData) {
        // 执行注册并获取结果
        Result<User> result = userService.register(userName, password);
        // 根据返回代码设置提示信息等
        String message = CODE.Message(result.getCode());
        // 如果注册成功，需要重新登录
        if (result.isSuccess()) {
            LOG.info(G.getCurClassMethodName() + " " + message);
            return "redirect:/login";
        }
        // 设置 message 用来返回到前端，一般登陆失败才会设置
        LOG.error(G.getCurClassMethodName() + " " + message);
        returnData.put("message", message);
        return "sign";
    }

    @GetMapping(path = "/login")  // 登陆页面，浏览器发起的是 GET 请求
    public String loginPage(Model model) {
        return "login";
    }

    @PostMapping(path = "/login")
    public String login(@RequestParam String userName,
                        @RequestParam String password,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        Map<String, Object> returnData) {
        // 执行登录并获取结果
        Result<User> result = userService.login(userName, password);
        // 根据返回代码设置提示信息等
        String message = CODE.Message(result.getCode());
        // 如果登录成功那么设置登录信息
        if (result.isSuccess()) {
            UserLoginInfo userLoginInfo = new UserLoginInfo();
            userLoginInfo.setUserId(String.valueOf(result.getData().getId()));
            userLoginInfo.setLoginName(result.getData().getUserName());
            LOG.info(G.getCurClassMethodName() + " " + message);
            // 取得 HttpSession 对象
            HttpSession session = request.getSession();
            session.setAttribute("userLoginInfo", userLoginInfo);
            return "redirect:/homePage";
        }
        // 设置 message 用来返回到前端，一般登陆失败才会设置
        LOG.error(G.getCurClassMethodName() + " " + message);
        returnData.put("message", message);
        return "login";
    }

}
